﻿package mortal.game.view.signUp.lineOn.data
{
    import Message.Game.*;
    import com.gengine.core.frame.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.signUp.data.*;
    import mortal.mvc.core.*;

    public class LineOnRewardCache extends Object
    {
        public var rewardInfo:STodayOnlineTimeInfo;
        public var getIndex:int = -1;
        public var isFirst:Boolean = true;
        public var firstOnTime:uint;
        public var timeLeftArr:Array;
        private var _hasGetFlag:Boolean = false;
        private var _secTimer:SecTimer;
        private var _zroSecTimer:SecTimer;
        public var ignorCk:Boolean;
        public var oldIgnor:Boolean = false;
        public var getGodTips:String = "";
        private var _synNum:int = 10;
        public static const timeArr:Array = [5, 15, 30, 60];

        public function LineOnRewardCache()
        {
            this.ignorCk = this.oldIgnor;
            this.timeLeftArr = [0, 0, 0, 0];
            this._secTimer = new SecTimer();
            this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimeHd);
            this._zroSecTimer = new SecTimer(30);
            this._zroSecTimer.addListener(TimerType.ENTERFRAME, this.onZeroTimeHd);
            return;
        }// end function

        public function get hasGetFlag() : Boolean
        {
            return this._hasGetFlag;
        }// end function

        public function set hasGetFlag(param1:Boolean) : void
        {
            this._hasGetFlag = param1;
            if (this._secTimer)
            {
                if (param1 == false)
                {
                    this._secTimer.start();
                }
                else
                {
                    this._secTimer.stop();
                }
            }
            return;
        }// end function

        public function getMaxTime() : uint
        {
            return timeArr[(timeArr.length - 1)];
        }// end function

        public function initData(param1:STodayOnlineTimeInfo) : void
        {
            var _loc_3:Object = null;
            if (this.isFirst)
            {
                this.isFirst = false;
                this._secTimer.start();
                this.rewardInfo = param1;
                this.firstOnTime = getTimer();
                Dispatcher.dispatchEvent(new DataEvent(EventName.LineOnInitData));
                this._zroSecTimer.start();
            }
            else
            {
                _loc_3 = this.getAdd(param1);
                this.hasGetFlag = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.LineOnUpdateOneData, _loc_3));
            }
            var _loc_2:* = this.hasLineOnReward();
            Cache.instance.signUp.hasRewardFlagObject[SignUpTabType.LineOnGetReward] = _loc_2;
            Dispatcher.dispatchEvent(new DataEvent(EventName.SignUpCanGetReward, ["signUp", SignUpTabType.LineOnGetReward, _loc_2]));
            return;
        }// end function

        private function printTime() : void
        {
            return;
        }// end function

        private function onTimeHd(param1:Object) : void
        {
            this.hasLineOnReward();
            return;
        }// end function

        private function onZeroTimeHd(param1:Object) : void
        {
            if (this.isZeroTime() == false)
            {
                return;
            }
            this.toZeroHd();
            return;
        }// end function

        public function toZeroHd() : void
        {
            var _loc_1:* = new STodayOnlineTimeInfo();
            _loc_1.getReward = new Dictionary();
            this.isFirst = false;
            this._secTimer.start();
            this.rewardInfo = _loc_1;
            this.firstOnTime = getTimer();
            this.hasGetFlag = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.LineOnInitData));
            var _loc_2:Boolean = false;
            Cache.instance.signUp.hasRewardFlagObject[SignUpTabType.LineOnGetReward] = _loc_2;
            Dispatcher.dispatchEvent(new DataEvent(EventName.SignUpCanGetReward, ["signUp", SignUpTabType.LineOnGetReward, _loc_2]));
            return;
        }// end function

        private function isZeroTime() : Boolean
        {
            var _loc_1:* = ClockManager.instance.nowDate;
            if (_loc_1.hours == 23)
            {
                if (_loc_1.minutes == 59)
                {
                    if (_loc_1.seconds > 59 - 20)
                    {
                        return true;
                    }
                }
            }
            else if (_loc_1.hours == 0)
            {
                if (_loc_1.minutes == 0)
                {
                    if (_loc_1.seconds < 20)
                    {
                        return true;
                    }
                }
            }
            return false;
        }// end function

        public function hasLineOnReward() : Boolean
        {
            var _loc_1:int = 0;
            var _loc_2:uint = 0;
            if (this.rewardInfo)
            {
                if (this.hasGetFlag == false)
                {
                    _loc_1 = 0;
                    _loc_1 = 0;
                    while (_loc_1 < timeArr.length)
                    {
                        
                        _loc_2 = timeArr[_loc_1];
                        if (this.getLineOnTime() >= _loc_2 * 60)
                        {
                            if (!this.rewardInfo.getReward[_loc_2])
                            {
                                this.hasGetFlag = true;
                                Cache.instance.signUp.hasRewardFlagObject[SignUpTabType.LineOnGetReward] = true;
                                Dispatcher.dispatchEvent(new DataEvent(EventName.SignUpCanGetReward, ["signUp", SignUpTabType.LineOnGetReward, true]));
                                return true;
                            }
                        }
                        else
                        {
                            break;
                        }
                        _loc_1++;
                    }
                    if (timeArr.length == _loc_1)
                    {
                        this._secTimer.stop();
                    }
                }
            }
            return false;
        }// end function

        private function setRealTime() : void
        {
            return;
        }// end function

        public function getLineOnTime() : int
        {
            var _loc_1:uint = 0;
            var _loc_2:uint = 0;
            if (this.rewardInfo)
            {
                _loc_1 = getTimer();
                _loc_2 = (_loc_1 - this.firstOnTime) / 1000;
                return this.rewardInfo.todayOnlineTime + _loc_2;
            }
            return 0;
        }// end function

        public function setResTime() : void
        {
            var _loc_1:int = 0;
            var _loc_2:uint = 0;
            var _loc_3:int = 0;
            _loc_1 = 0;
            while (_loc_1 < timeArr.length)
            {
                
                _loc_2 = timeArr[_loc_1];
                _loc_3 = _loc_2 * 60 - this.getLineOnTime();
                _loc_3 = Math.ceil(_loc_3 / 60);
                this.timeLeftArr[_loc_1] = _loc_3;
                _loc_1++;
            }
            return;
        }// end function

        private function getAdd(param1:STodayOnlineTimeInfo) : Object
        {
            var _loc_2:Object = null;
            var _loc_3:String = null;
            var _loc_4:Object = null;
            if (this.rewardInfo == null || param1 == null)
            {
                return null;
            }
            for (_loc_3 in param1.getReward)
            {
                
                if (!this.rewardInfo.getReward[int(_loc_3)])
                {
                    this.rewardInfo.getReward = param1.getReward;
                    _loc_4 = LineOnRewardConfig.instance.getConstObjByNum(int(_loc_3));
                    return {key:int(_loc_3), constValue:_loc_4.constValue, itemCode:param1.getReward[_loc_3]};
                }
            }
            return _loc_2;
        }// end function

        public function findCanGetIndex() : int
        {
            var _loc_2:uint = 0;
            var _loc_1:int = 0;
            _loc_1 = 0;
            while (_loc_1 < timeArr.length)
            {
                
                _loc_2 = timeArr[_loc_1];
                if (this.getLineOnTime() >= _loc_2 * 60)
                {
                    if (!this.rewardInfo.getReward[_loc_2])
                    {
                        return _loc_1;
                    }
                }
                else
                {
                    return _loc_1;
                }
                _loc_1++;
            }
            return 0;
        }// end function

    }
}
